package com.hzdy.master.utils;

import com.hzdy.master.entity.Detail;
import com.hzdy.master.entity.table.CreateTable;
import com.squareup.javapoet.*;
import lombok.Getter;
import lombok.Setter;
import org.springframework.data.jpa.repository.JpaRepository;

import javax.lang.model.element.Modifier;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.tools.JavaCompiler;
import javax.tools.JavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;
import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
import org.springframework.stereotype.Repository;

public class EntityUtils {

    public static TypeSpec buildRepository(String className) throws ClassNotFoundException {
        String[] classLoaction=className.split(".");

        TypeSpec.Builder entityBuilder = TypeSpec
                .interfaceBuilder(String.format("%sRepository","Person"))
                .addSuperinterface(ParameterizedTypeName.get(ClassName.get(JpaRepository.class),
                        ClassName.get(Class.forName(className)), ClassName.get(Long.class)))
                .addAnnotation(Repository.class)
                .addModifiers(Modifier.PUBLIC);
        return entityBuilder.build();

    }

    public static TypeSpec build(CreateTable createTable) {
        AnnotationSpec tableSpec = AnnotationSpec
                .builder(Table.class)
                .addMember("name", String.format("\"%s\"",createTable.getTableName()))
                .build();
        TypeSpec.Builder entityBuilder = TypeSpec
                .classBuilder(createTable.getClassName())
                .addSuperinterface(Serializable.class)
                .superclass(Detail.class)
                .addAnnotation(tableSpec)
                .addAnnotation(Entity.class)
                .addModifiers(Modifier.PUBLIC);
        createTable.getFormFields().forEach(field -> {
            String fieldName=field.getFieldName();
            String captureFieldName=captureName(fieldName);
            FieldSpec info = FieldSpec
                    .builder(format(field.getFieldType()), fieldName)
                    .addModifiers(Modifier.PRIVATE)
                    .build();
            entityBuilder.addField(info);
            entityBuilder.addMethod(MethodSpec
                    .methodBuilder(String.format("get%s",captureFieldName))
                    .addModifiers(Modifier.PUBLIC)
                    .returns(format(field.getFieldType()))
                    .addStatement(String.format("return this.%s",fieldName))
                    .build())
                    .addMethod(MethodSpec
                            .methodBuilder(String.format("set%s",captureFieldName))
                            .addParameter(String.class, fieldName)
                            .addModifiers(Modifier.PUBLIC)
                            .addStatement(String.format("this.%s = %s",fieldName,fieldName)).build());
        });
        return entityBuilder.build();
    }

    public static String captureName(String name){
        char[] cs=name.toCharArray();
        cs[0]-=32;
        return String.valueOf(cs);
    }
    public static Class<?> format(String type) {
        switch (type) {
            case "int":
                return int.class;
            case "String":
                return String.class;
            default:
                throw new NullPointerException("给老子设置类型!");
        }
    }

    public static void compile(String locationName,String className){
        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
        StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
        File file = new File(String.format("%s/%s",locationName,className));
        Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjects(file);
        //生成编译任务
        JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, null, null, null, compilationUnits);
        //执行编译任务
        task.call();
    }
}
